package com.example.suuu.project.system.service.impl;

import cn.hutool.core.lang.ObjectId;
import com.example.suuu.project.system.entity.SuRole;
import com.example.suuu.project.system.entity.SuRoleMenu;
import com.example.suuu.project.system.entity.SuUserRole;
import com.example.suuu.project.system.entity.vo.RoleMenuCode;
import com.example.suuu.project.system.entity.vo.UserRoleCode;
import com.example.suuu.project.system.mapper.SuRoleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.suuu.project.system.service.SuRoleService;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import com.example.suuu.model.PagVo;
import com.example.suuu.project.system.query.SuRoleQuery;

/**
* <p>
    * 角色 服务实现类
    * </p>
*
* @author sun
* @date 2023-06-28
*/
@Transactional
@Service
public class SuRoleServiceImpl extends ServiceImpl<SuRoleMapper, SuRole> implements SuRoleService{

    @Resource
    private SuRoleMapper suRoleMapper;


    @Override
    public int insertBatch(RoleMenuCode roleMenuCode) {
        List<SuRoleMenu> u = new ArrayList<>();
        for (String m : roleMenuCode.getMenucodes()) {
            SuRoleMenu r = new SuRoleMenu();
            r.setRolecode(roleMenuCode.getRolecode());
            r.setMenucode(m);
            u.add(r);
        }
        suRoleMapper.delmenucode(roleMenuCode.getRolecode());
        return suRoleMapper.insertBatch(u);
    }

    /**
    * 查询全部
    */
    @Override
    public List<SuRole> getList(SuRole u){
        return suRoleMapper.getList(u);
    };
    /**
    * 查询总数
    */
    @Override
    public Long count(SuRole u){
        return suRoleMapper.count(u);
    };
    /**
    * 查询单个
    */
    @Override
    public SuRole getById(Integer id){
        return suRoleMapper.getById(id);
    };
    /**
    * 新增
    */
    @Override
    public int add(SuRole u){
        u.setIsdel(1);
        u.setRolecode(ObjectId.next());
        u.setCreatetime(LocalDateTime.now());
        u.setUpdatetime(LocalDateTime.now());
        return suRoleMapper.add(u);
    };
    /**
    * 修改
    */
    @Override
    public int update(SuRole u){
        u.setUpdatetime(LocalDateTime.now());
        return suRoleMapper.update(u);
    };
    /**
    * 删除
    */
    @Override
    public int del(Integer id){
        return suRoleMapper.del(id);
    };

    /**
    * 分页区分方法
    */
    @Override
    public Object pagVo(SuRoleQuery query){
        SuRole u = new SuRole();
        u.setIsdel(1);
        if (query.getCurrentPage() != null && query.getPageSize() != null) {
            u.setCurrentPage((query.getCurrentPage()-1)*query.getPageSize());
            u.setPageSize(query.getPageSize());
            PagVo p = new PagVo();
            p.setCount(count(u));
            p.setList(getList(u));
            return p;
        }
        return getList(u);
    };
}